package com.eh.taxation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eh.commonutils.R;
import com.eh.taxation.entity.Subject;
import com.eh.taxation.service.SubjectService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 问题类别 前端控制器
 * </p>
 *
 * @author erhuo
 * @since 2021-01-21
 */
@RestController
@Api(value = "SubjectController", tags = "分类管理")
@RequestMapping("/taxation/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @GetMapping("/getAllSubject")
    public R getAllSubject() {
        QueryWrapper<Subject> oneSubjectWrapper = new QueryWrapper<>();
        oneSubjectWrapper.eq("parent_id", 0);
        List<Subject> oneSubjectList = subjectService.list(oneSubjectWrapper);
        QueryWrapper<Subject> twoSubjectWrapper = new QueryWrapper<>();
        twoSubjectWrapper.ne("parent_id", 0);
        List<Subject> twoSubjectList = subjectService.list(twoSubjectWrapper);
        for (Subject subject : oneSubjectList) {
            ArrayList<Subject> list = new ArrayList<>();
            String onsubjectId = subject.getId();
            for (Subject twosubject : twoSubjectList) {
                if (twosubject.getParentId().equals(onsubjectId)) {
                    list.add(twosubject);
                }
            }
            subject.setChildren(list);
        }
        return R.ok().data("list", oneSubjectList);
    }

    // 添加课程分类
    // 获取上传过来的文件，把文件内容读取出来
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file) {
        subjectService.saveSubject(file, subjectService);
        return R.ok();
    }
}

